<?php
/**
 * s3CinchUtils
 *
 * basic utils class for s3Cinch
 *
 * @category   Utils
 * @package    s3Cinch v1.0
 * @author     Stephen Gray <stephen@zgsolutions.co.uk>
 * @copyright  2010 Stephen Gray
 * @since      Mon 9 Jan 10 21:57
 */
class s3CinchUtils
{
    /**
     * void log ( string $filePath , string $logMessage )
     *
     * log a message to a given file with some
     * basic stamp info
     *
     * @param  string $filePath
     * @param  string $logMessage
     * @return void
     *
     * @access public static
     */
    public static function log($filePath, $logMessage)
    {
        $fileHandle = fopen($filePath, 'a');
        if (!$fileHandle)
        {
            throw new Exception('Failed to open following log file, please check permissions: '.$filePath);
        }

        echo $logMessage."\n";
        $message = '['.date('d-m-Y \\a\\t H:i:s').'] '.$logMessage."\n";
        fwrite($fileHandle, $message);
        fclose($fileHandle);
    }

    /**
     * bool scheduleDueNow ( string $schedule )
     *
     * check if a file is due by it's schedule
     *
     * @param  string $schedule
     * @return bool
     *
     * @access public static
     */
    public static function scheduleDueNow($schedule)
    {
        $dueNow = false;
        switch ($schedule)
        {
            /**
             * true if time is on the hour
             */
            case 'hourly':
                $dueNow = date('i') == '00' ? true : false;
                break;
            /**
             * true if hour and minute are
             * both 00
             */
            case 'daily':
                if (date('i') == '00' && date('G') == '0')
                {
                    $dueNow = true;
                }
                break;
            /**
             * true if above and also day of week
             * is 1 (Monday)
             */
            case 'weekly':
                if (date('i') == '00' && date('G') == '0' && date('N') == '1')
                {
                    $dueNow = true;
                }
                break;
            /**
             * true if hour, minute are 0 and day
             * of month is 1
             */
            case 'monthly':
                if (date('i') == '00' && date('G') == '0' && date('j') == '1')
                {
                    $dueNow = true;
                }
                break;
        }
        return $dueNow;
    }
}
